package mysql

import "github.com/beego/beego/v2/core/utils"

type SonQueryBuilder struct {
	Fields    []string
	Arguments map[string]SonQueryArguments
}

func (sqb SonQueryBuilder) AddField(name string) *SonQueryArguments {
	sqb.Fields = append(sqb.Fields, name)
	sqa := SonQueryArguments{}
	sqa.UseDefaultRelationDepth = true
	sqa.LoadRelationDepth = 0
	sqa.Limit = 1000
	sqa.Offset = 0
	sqa.Order = "-Id"
	sqb.Arguments[name] = sqa
	return &sqa
}
func (sqb SonQueryBuilder) GetArguments(name string) []interface{} {
	args, ok := sqb.Arguments[name]
	if ok {
		return args.GetArguments()
	} else {
		return nil
	}
}

type SonQueryArguments struct {
	UseDefaultRelationDepth bool
	LoadRelationDepth       int
	Limit                   int
	Offset                  int
	Order                   string
}

func (sqa SonQueryArguments) UseDefaultDepth() *SonQueryArguments {
	sqa.UseDefaultRelationDepth = true
	sqa.LoadRelationDepth = -1
	return &sqa
}
func (sqa SonQueryArguments) SetDepth(depth int) *SonQueryArguments {
	sqa.UseDefaultRelationDepth = false
	sqa.LoadRelationDepth = depth
	return &sqa
}
func (sqa SonQueryArguments) SetOffset(offset int) *SonQueryArguments {
	sqa.Offset = offset
	return &sqa
}
func (sqa SonQueryArguments) SetLimit(limit int) *SonQueryArguments {
	sqa.Limit = limit
	return &sqa
}
func (sqa SonQueryArguments) SetOrder(order string) *SonQueryArguments {
	sqa.Order = order
	return &sqa
}
func (sqa SonQueryArguments) GetArguments() []interface{} {
	var args []interface{}
	if sqa.UseDefaultRelationDepth == true {
		args = append(args, true)
	} else {
		args = append(args, sqa.LoadRelationDepth)
	}
	args = append(args, sqa.Limit)
	args = append(args, sqa.Offset)
	args = append(args, sqa.Order)
	return args
}

type OrmRelateArgument struct {
	Key   interface{}
	Value interface{}
}

func (s *OrmRelateArgument) GetKey() interface{} {
	return s.Key
}

func (s *OrmRelateArgument) GetValue() interface{} {
	return s.Value
}

type OrmRelateParam struct {
	Args []utils.KV
}

func (s *OrmRelateParam) Add(key interface{}, value interface{}) {
	arg := new(OrmRelateArgument)
	arg.Key = key
	arg.Value = value
	s.Args = append(s.Args, arg)
}
